// tools to exclude from the normal functionality of tool click-action
var ToolState = ['zoomfullext', 'zoomprev', 'zoomnext', 'draw-clear', 'draw-done'];


// magic number: width of the sidebar
var sidebarw = 300;
// magic number: height of the branding/logo div
var logoh = 49;
// magic number: height of the branding/logo div
var headh = 92;
// magic number: height of the layers button div
var layersh = 0;


window.addEvent('domready', function() {


    // navigation tab links action
    if (!printmode) FIUMAPS.Tabs.initiateTabs();

    current_tab = $('LocationsByName');
    // el - is the new element being added
    // grow - the shrink/grow adjustment being performed
    // grow = true will grow accordion (defaults to true)
    // grow = false will shrink accordion

    accordionResize = function(el, grow) {
        var oh = $$('.element.active').getHeight(); // old height of the currently active accordion panel
        $$('.element.active').setStyle('height', oh); // set height so that when element is displayed the height of currently active accordion panel is not changed...yet
        if (grow == true || grow == undefined) {
            $(el).setStyle('display', 'block'); // display the element
        }
        else {
            $(el).setStyle('display', 'none'); // remove the element
        }
        var nh = $$('.element.active .accordion-box').getHeight(); // new height set to a div inside of the currently active accordion panel
        $$('.element.active').tween('height', [oh, nh]); // adjust the accordion panel to the new height


    };
    //    $('zoomin').addEvent('click', function(event){
    //        accordionResize('layertest2');
    //    });
    //    $('zoomout').addEvent('click', function(event){
    //        accordionResize('layertest2',false);
    //    });
    var accordion;
    // locations by category
    //	$('LocationsByCategory').addEvent('click', function(event){
    //        // clear map overlays 
    //        FIUMAPS.LocationsByCategory.ResetCategoryStyles();
    //        mymapLocations.hideGeometries();
    //        $('searchbox').value = "";
    //	});	

    $('LocationsByName').addEvent('click', function(event) {
        //mymapLocations.hideGeometries();
        FIUMAPS.LocationsByName.Reset();
        current_tab = $('LocationsByName');
    });

    $('MapLayers').addEvent('click', function(event) {
        mymapLocations.hideGeometries();
        current_tab = $('MapLayers');
    });
    // locations by category
    $('myLocationsHeader').addEvent('click', function(event) {
        mymapLocations.showGeometries();
        $('searchbox').value = "";
        current_tab = $('myLocationsHeader');
    });

    // search box actions
    $('searchbox').addEvent('focus', function(event) {
        var searchvalue = this.get('value');
        if (searchvalue == "search the map" || searchvalue == "") {
            this.set('value', '');
            showSearchInstructions();
        }
        mymapLocations.hideGeometries();
        current_tab = $('searchbox');
    });
    $('searchbox').addEvent('blur', function(event) {
        var searchvalue = this.get('value');
        if (searchvalue == "") this.set('value', 'search the map');
    });

    $('searchbutton').addEvent('click', function() {
        FIUMAPS.Search.ExecuteFind($('searchbox').value);
        showSearchProgress();
    });


    // magic number: width of the sidebar
    var sidebarw = 300;
    // magic number: height of the branding/logo div
    var logoh = 49;
    // magic number: height of the layers button div
    var layersh = 0;

    //var hwrap = window.getScrollHeight();
    var hwrap = window.getHeight();
    //alert(window.getHeight());
    var wwrap = window.getWidth();
    $('accordion').setStyle('height', (hwrap - logoh - layersh) + 'px');
    $('mapid').setStyle('width', (wwrap - sidebarw - 1) + 'px');
    $('mapid').setStyle('height', (hwrap - headh) + 'px');
    $('menu').setStyle('width', wwrap + 'px');


    // resize the components on window resize
    window.onresize = componentResizing;

    // set up the accordion, but put a bit of a delay on it so that
    // its height is properly established
    if (URLDecode(getQuerystring('sids')) || URLDecode(getQuerystring('bids'))) {
        (function() {
            accordion = new Accordion($$('.toggler'), $$('.element'), {
                opacity: 1,
                display: 4,
                onActive: function(toggler, element) {
                    toggler.removeClass('deactive');
                    toggler.addClass('active');
                    element.removeClass('deactive');
                    element.addClass('active');
                },
                onBackground: function(toggler, element) {
                    toggler.removeClass('active');
                    toggler.addClass('deactive');
                    element.removeClass('active');
                    element.addClass('deactive');
                }
            });
        }).delay(5);
    } else {
        (function() {
            accordion = new Accordion($$('.toggler'), $$('.element'), {
                opacity: 1,
                display: 2,
                onActive: function(toggler, element) {
                    toggler.removeClass('deactive');
                    toggler.addClass('active');
                    element.removeClass('deactive');
                    element.addClass('active');
                },
                onBackground: function(toggler, element) {
                    toggler.removeClass('active');
                    toggler.addClass('deactive');
                    element.removeClass('active');
                    element.addClass('deactive');
                }
            });
        }).delay(5);
    }





    // make pan default tool when page loads
    //	$('pan').addClass('active');

    // add class=active when a toolbox icon is enabled, then remove it when another or the same one is clicked again
    //	$('navToolbar').getElements('li').addEvent('click', function(event){
    //		// ask if li has the 'active' class
    //		var has = this.hasClass('active');
    //		// store the tools that don't require a change in state when clicked
    //		var nostate = ToolState.indexOf(this.id);
    //		if (has == false && nostate < 0) {
    //			$('navToolbar').getElements('li').removeClass('active');
    //			this.addClass('active');
    //		}
    //		else 
    //			if (nostate >= 0) {
    //				this.removeClass('active');
    //			}
    //	});


    //	// add down state for toolbar items that don't have a persistant state
    //	$('navToolbar').getElements('li').addEvent('mousedown', function(event){
    //		// store the tools that require a mousedown change in state only
    //		var state = ToolState.indexOf(this.id);
    //		if (state >= 0) {
    //			this.addClass('down');
    //		}
    //	});
    //	
    //	
    //	// add up state for toolbar items that don't have a persistant state
    //	$('navToolbar').getElements('li').addEvent('mouseup', function(event){
    //		// store the tools that require a mousedown change in state only
    //		var state = ToolState.indexOf(this.id);
    //		if (state >= 0) {
    //			this.removeClass('down');
    //		}
    //	});


    //	// toggle the map view
    //	$('map-type').getElements('a').addEvent('click', function(e){
    //		// PUT CALL TO CHANGE VIEW HERE
    //		
    //		$('map-type').getElements('a').removeClass('active');
    //		this.addClass('active');
    //	});



    // adjust the height of the container div with a smooth transition
    // pass in element to be adjusted/resized, and the element that will be appearing/displayed
    // twk - tweek adjustment amount, dir - direction, add or subtract from the size of the container
    smoothAdjAccordion = function(el1, el2, twk, dir) {
        if (twk == undefined) {
            twk = 0;
        }
        var h1 = (el1.getHeight()) + twk;
        var h2 = el2.getHeight();
        if (dir == undefined || dir == "add") {
            var newh = (h1 + h2);
        }
        else {
            var newh = (h1 - h2);
        }
        el1.tween('height', [h1, newh]);
    };



    FIUMAPS.ModalWindow.hide();


});

// resize the various map components if the browser window size is altered
function componentResizing(){
	var hwrap = window.getHeight();
	var wwrap = window.getWidth();
	$('accordion').setStyle('height', (hwrap - logoh - layersh) + 'px');
	$('mapid').setStyle('width', (wwrap - sidebarw - 1) + 'px');
	$('mapid').setStyle('height', (hwrap - headh) + 'px');
	$('menu').setStyle('width', wwrap + 'px');
	// 'mapid' var is set in init.js so comment this out
	//map.resize();
}

//blank function for use on hrefs that are javascript controlled
function blank(){
	// nothing goes here...
}
